// ------------------------------------------------------------------------------
// 
// Copyright (c) 2008-2009 Swampware, Inc.
// 
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
// 
// ------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;

namespace Bamboo.Parsing.Generators.CPlusPlus
{
	public class TokenGenerator
	{

		public static void GenerateHeader(string name, string multinamespace, Bamboo.Parsing.FiniteAutomata.FiniteAutomaton finiteAutomaton, System.IO.TextWriter writer)
		{
			writer.WriteLine("//");
			writer.WriteLine("// " + name + "Token.h");
			writer.WriteLine("//");
			writer.WriteLine("// AUTOGENERATED " + System.DateTime.Now + "");
			writer.WriteLine("//");
			writer.WriteLine("");
			writer.WriteLine("#ifndef " + name.ToUpper() + "TOKEN_H");
			writer.WriteLine("#define " + name.ToUpper() + "TOKEN_H");
			writer.WriteLine("");
			writer.WriteLine("#include <iostream>");
			writer.WriteLine("#include <string>");
			writer.WriteLine("#include \"" + name + "TokenType.h\"");
			writer.WriteLine("");

			string[] namespaces = multinamespace.Split(new string[] { "::" }, StringSplitOptions.None);
			foreach (string nspace in namespaces)
			{
				writer.WriteLine("namespace " + nspace + "");
				writer.WriteLine("{");
			}

			writer.WriteLine("	class " + name + "Token");
			writer.WriteLine("	{");
			writer.WriteLine("	private:");
			writer.WriteLine("		static int _counter;");
			writer.WriteLine("");
			writer.WriteLine("		int _type;");
			writer.WriteLine("		std::string _value;");
			writer.WriteLine("	public:");
			writer.WriteLine("		" + name + "Token(int type) : _type(type) { _counter++; }");
			writer.WriteLine("		" + name + "Token(int type, std::string value) : _type(type), _value(value) { _counter++; }");
			writer.WriteLine("		~" + name + "Token() { _counter--; }");
			writer.WriteLine("		static int counter() { return _counter; }");
			writer.WriteLine("");
			writer.WriteLine("		int type() { return _type; }");
			writer.WriteLine("		std::string value() { return _value; }");
			writer.WriteLine("");
			writer.WriteLine("		friend std::ostream& operator<<(std::ostream& stream, Token& token) { ");
			writer.WriteLine("			stream << \"TOKEN \" << token._type;");
			writer.WriteLine("			if(!token._value.empty())");
			writer.WriteLine("				stream << \" \" << token._value;");
			writer.WriteLine("			return stream << std::endl; }");
			writer.WriteLine("		friend std::ostream& operator<<(std::ostream& stream, Token* token) { ");
			writer.WriteLine("			stream << \"TOKEN \" << token->_type;");
			writer.WriteLine("			if(!token->_value.empty())");
			writer.WriteLine("				stream << \" \" << token->_value;");
			writer.WriteLine("			return stream << std::endl; }");
			writer.WriteLine("	};");

			foreach (string nspace in namespaces)
			{
				writer.WriteLine("}");
			}
			writer.WriteLine("#endif");
		}

		public static void GenerateClass(string name, string multinamespace, Bamboo.Parsing.FiniteAutomata.FiniteAutomaton finiteAutomaton, System.IO.TextWriter writer)
		{
			writer.WriteLine("//");
			writer.WriteLine("// " + name + "Token.cpp");
			writer.WriteLine("//");
			writer.WriteLine("// AUTOGENERATED " + System.DateTime.Now + "");
			writer.WriteLine("//");
			writer.WriteLine("");
			writer.WriteLine("#include \"" + name + "Token.h\"");
			writer.WriteLine("");

			string[] namespaces = multinamespace.Split(new string[] { "::" }, StringSplitOptions.None);
			foreach (string nspace in namespaces)
			{
				writer.WriteLine("namespace " + nspace + "");
				writer.WriteLine("{");
			}

			writer.WriteLine("	int " + name + "Token::_counter = 0;");
			writer.WriteLine("");

			foreach (string nspace in namespaces)
			{
				writer.WriteLine("}");
			}
		}

		private TokenGenerator()
		{
		}

	}
}
